using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Application.Utils;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    public class AddStudentController : ControllerBase
    {
        private readonly IRepository<Students> _Students;
        private readonly IRepository<College> _College;
        private readonly IRepository<Major> _Major;
        private readonly IRepository<Avatar> _Avatar;
        private readonly IRepository<Class> _Class;
        private readonly IRepository<StuUsers> _StuUsers;
        private readonly IRepository<MajorChangeSave> _MajorChangeSave;

        public AddStudentController(IRepository<MajorChangeSave> MajorChangeSave, IRepository<StuUsers> StuUsers, IRepository<Avatar> Avatar, IRepository<Class> Class, IRepository<Major> Major, IRepository<College> College, IRepository<Students> Students)
        {
            _MajorChangeSave = MajorChangeSave;

            _StuUsers = StuUsers;

            _Avatar = Avatar;

            _Class = Class;

            _Major = Major;

            _College = College;

            _Students = Students;
        }

        //删除学生
        [HttpDelete("/GetDeleteStu/{Id}")]
        public async Task<object> DeleteStu(string Id)
        {
            var stu = _Students.Table.FirstOrDefault(x => x.StudentId == Id);

            if (stu == null)
            {
                return new
                {

                    code = "400",
                    data = "",
                    msg = "未找到该学生"

                };

            }

            var user = _StuUsers.Table.FirstOrDefault(x => x.UserId == Id);

            if (user == null)
            {
                return new
                {

                    code = "400",
                    data = "",
                    msg = "未找到该账户"

                };

            }

            try
            {
                await _Students.DeleteAsync(stu.Id,true);

                await _StuUsers.DeleteAsync(user.Id,true);

                var data = _Students.Table;

                return new
                {

                    code = "200",
                    data ,
                    msg = "注销成功"

                };
            }
            catch
            {
                return new
                {

                    code = "400",
                    data = "",
                    msg = "注销失败"

                };
            }


        }


        //批量添加学生
        [HttpPost("/GetUpdateStudent")]
        public async Task<object> UpdateStudent(StudentDto studentDto)
        {

            var student = _Students.Table.FirstOrDefault(x => x.StudentId == studentDto.StudentId);

            if (student != null)
            {

                student.StudentId = studentDto.StudentId;
                student.StudentName = studentDto.StudentName;
                student.FatherName = studentDto.FatherName;
                student.MontherName = studentDto.FatherName;
                student.Sex = studentDto.Sex;
                student.IdentityNumber = studentDto.IdentityNumber;
                student.PhoneNumber = studentDto.PhoneNumber;
                student.ChattingNumber = studentDto.ChattingNumber;
                student.QQNumber = studentDto.QQNumber;
                student.Address = studentDto.Address;
                student.FatherPhone = studentDto.FatherPhone;
                student.MontherPhone = studentDto.MontherPhone;
                student.ClassId = studentDto.ClassId;

                try
                {
                    await _Students.UpdateAsync(student);

                    var students = _Students.Table;

                    if (student.ClassId != studentDto.ClassId)
                    {

                        var studentChange = _MajorChangeSave.Table.FirstOrDefault(X => X.StudentId == studentDto.StudentId);



                        if (studentChange != null)
                        {
                            return new
                            {

                                code = "400",
                                data = students,
                                msg = "该学生已经失去更改专业的机会了哦！丢了西瓜捡了芝麻可不是什么好事哦"

                            };
                        }

                        var data = new MajorChangeSave
                        {

                            StudentId = studentDto.StudentId,
                            TermId = studentDto.TermId,
                            ClassId = studentDto.ClassId,

                        };

                        await _MajorChangeSave.AddAsync(data);

                    }

                    return new
                    {

                        code = "200",
                        data = students,
                        msg = "更新成功"

                    };
                }
                catch
                {
                    return new
                    {

                        code = "400",
                        data = "",
                        msg = "更新失败"

                    };
                }

            }
            else


                return new
                {

                    code = "400",
                    data = "",
                    msg = "错误信息"

                };


        }


        //批量添加学生
        [HttpPost("/AddStudent")]
        public async Task<object> BatchAddStudents(List<Students> studentDto)
        {

            if (studentDto.Count > 0)
            {
                var classid = studentDto[0].ClassId != null ? studentDto[0].ClassId : new Guid();

                var classes = _Class.Table.OrderBy(x => x.CreatedAt).ToList();

                var students = _Students.Table.OrderBy(x => x.CreatedAt).ToList();

                var date = DateTime.Now.Year % 100;

                var aclass = classes.Find(x => x.Id == classid);

                if (aclass != null)
                {

                    var classId = classes.IndexOf(aclass) + 1;

                    var StrclassId = classId / 10 > 0 ? classId.ToString() : "0" + classId;

                    var majors = _Major.Table.OrderBy(x => x.CreatedAt).ToList();

                    var major = majors.Find(x => x.Id == aclass.MajorId);

                    if (major == null)

                        return new
                        {

                            code = "400",
                            data = "",
                            msg = "无效的专业信息"

                        };


                    var majorId = majors.IndexOf(major) + 1;

                    var StrmajorId = majorId / 10 > 0 ? majorId.ToString() : "0" + majorId;

                    var colleges = _College.Table.OrderBy(x => x.CreatedAt).ToList();

                    var college = colleges.Find(x => x.Id == major.CollegeId);

                    if (college == null)

                        return new
                        {

                            code = "400",
                            data = "",
                            msg = "无效的学院信息"

                        };

                    var collegeId = colleges.IndexOf(college) + 1;

                    var StrcollegeId = collegeId / 10 > 0 ? collegeId.ToString() : "0" + collegeId;

                    List<StuUsers> stUList = new List<StuUsers>();

                    var mAvatar = _Avatar.Table.FirstOrDefault(x => x.CurrentName == "002.jpg");

                    var wAvatar = _Avatar.Table.FirstOrDefault(x => x.CurrentName == "003.jpg");

                    var i = students.Where(x => x.ClassId == studentDto[0].ClassId).Select(x => new { x.Id }).Count() + 1;

                    foreach (var item in studentDto)
                    {

                        var IsIn = students.FirstOrDefault(x => x.IdentityNumber == item.IdentityNumber);

                        if (IsIn != null)


                            return new
                            {

                                code = "400",
                                data = "",
                                msg = "身份证为：" + item.IdentityNumber + "的同学已存在"

                            };



                        var StrId = i / 10 > 0 ? i.ToString() : "0" + i;

                        item.StudentId = date + StrcollegeId + StrmajorId + StrclassId + StrId;

                        if (item.Sex == "男")
                        {
                            var data = new StuUsers
                            {

                                UserId = item.StudentId,

                                AvatarId = mAvatar == null ? new Guid() : mAvatar.Id,

                                PassWord = "123456"
                            };

                            stUList.Add(data);

                        }
                        else if (item.Sex == "女")
                        {

                            var data = new StuUsers
                            {

                                UserId = item.StudentId,

                                AvatarId = wAvatar == null ? new Guid() : wAvatar.Id,

                                PassWord = "123456"
                            };

                            stUList.Add(data);

                        }
                        else
                        {

                            return new
                            {

                                code = "400",
                                data = "",
                                msg = item.StudentName + "存在无效的性别信息"

                            };
                        }

                        i++;
                    }
                    try
                    {
                        await _Students.AddBulkAsync(studentDto);

                        await _StuUsers.AddBulkAsync(stUList);

                        return new
                        {

                            code = "200",
                            data = "",
                            msg = "添加成功"

                        };
                    }
                    catch
                    {
                        return new
                        {

                            code = "400",
                            data = "",
                            msg = "未知错误"

                        };
                    }

                }
                else
                {

                    return new
                    {

                        code = "400",
                        data = "",
                        msg = "无效的班级信息"

                    };

                };

            }
            else

                return new
                {

                    code = "400",
                    data = "",
                    msg = "不可为空"

                };

        }
    }
}